package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.RedempOrderActionDetail;
import com.turing.post.salesmanage.util.RedempOrderActionDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 赎回单状态迁移明细
 * 
 * @author zxp
 * @Jan 29, 2013
 */
@Stateless(mappedName = "salesmanage.impl.RedempOrderActionDetailDaoImpl")
@Remote
@Local
public class RedempOrderActionDetailDaoImpl implements
		RedempOrderActionDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<RedempOrderActionDetail> getRedempOrderActionDetailList(
			BigInteger redempOrderId, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from RedempOrderActionDetail t where t.RedempOrderId="
				+ redempOrderId + " order by t.actionTime";
		Query query = _em.createNativeQuery(sql, RedempOrderActionDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getRedempOrderActionDetailTotal(BigInteger redempOrderId) {
		String sql = "select nvl(count(*),0) from RedempOrderActionDetail t where t.RedempOrderId="
				+ redempOrderId;
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertRedempOrderActionDetail(RedempOrderActionDetail road)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, road);
	}
}